package com.lianxueda.drawing.controller;

import com.lianxueda.drawing.common.R;
import com.lianxueda.drawing.domain.dto.CourseStyleQueryDTO;
import com.lianxueda.drawing.domain.dto.CourseStyleSaveDTO;
import com.lianxueda.drawing.domain.pojo.CourseStyle;
import com.lianxueda.drawing.domain.vo.PageVO;
import com.lianxueda.drawing.service.CourseStyleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @author 29264
 */
@RestController
@RequestMapping("/api/course/style")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "课程风采管理模块")
public class CourseStyleController {

    private final CourseStyleService courseStyleService;

    @PostMapping("/add")
    @Operation(summary = "新增课程风采")
    public R save(@RequestBody CourseStyleSaveDTO courseStyleSaveDTO){
        CourseStyle courseStyle = new CourseStyle();
        BeanUtils.copyProperties(courseStyleSaveDTO, courseStyle);
        courseStyle.setPublishTime(LocalDateTime.now());
        courseStyle.setViewCount(0);
        courseStyleService.save(courseStyle);
        return R.suc("课堂风采新增成功");
    }

    @PostMapping("/update")
    @Operation(summary = "更新课程风采")
    public R update(@RequestBody CourseStyleSaveDTO courseStyleSaveDTO){
        CourseStyle courseStyle = new CourseStyle();
        BeanUtils.copyProperties(courseStyleSaveDTO, courseStyle);
        courseStyleService.updateById(courseStyle);
        return R.suc("课堂风采更新成功");
    }

    @PostMapping("/updateById")
    @Operation(summary = "更新课程风采的观看次数")
    public R updateViewCount(@Parameter Integer id,@Parameter Integer viewCount ){
        courseStyleService.updateViewCount(id,viewCount);
        return R.suc("课堂风采观看次数更新成功");
    }

    @DeleteMapping("/delect")
    @Operation(summary = "删除课程风采")
    public R delete(@Parameter Integer id){
        courseStyleService.removeById(id);
        return R.suc("课堂风采删除成功");
    }


    @DeleteMapping("/deletes")
    @Operation(summary = "批量删除课程风采")
    public R deleteBatch(@RequestBody  Integer[] ids){
        courseStyleService.removeByIds(Arrays.asList(ids));
        return R.suc("批量删除课堂风采成功");
    }


    @PostMapping("/Pagelist")
    @Operation(summary = "分页查询课程风采")
    public R pageList(@RequestBody CourseStyleQueryDTO courseStyleQueryDTO){
        PageVO pageVo = courseStyleService.searchPage(courseStyleQueryDTO);
        return R.suc(pageVo);
    }

}
